package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.DashboardEntity;
import com.hllxd.hism.star.basic.service.service.DashboardService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/dashboard")
public class DashboardController2 {

    @Autowired
    private DashboardService dashboardService;

    @Data
    public static class Params {
        private Integer p1;
        private String ownerID;
        private String ptdate;
        /** [report, zhibiao]*/
        public String dashboardType;
        public Long id;
    }

    @RestGrpcMapping("/loadDashboard.do")
    public NTSTATUS loadDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            List<DashboardEntity> ret = dashboardService.loadDashboard(aa.dashboardType);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/addDashboard.do")
    public NTSTATUS addDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            DashboardEntity dashboard = params.driverObject(DashboardEntity.class);
            Object res = dashboardService.setDashboard(dashboard);
            log.info("result success.");
            result.setData(true);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getDashboard.do")
    public NTSTATUS getDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            DashboardEntity res = dashboardService.getDashboard(aa.id);
            log.info("result success.");
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


//    @RestGrpcMapping("/queryYarnQueue.do")
//    public NTSTATUS queryYarnQueue(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("params: {}", params);
//        try {
//            Params aa = params.driverObject(Params.class);
//            //
//            List<CostReportEntity> ret = dashboardService.queryYarnQueue(aa.ownerID, aa.ptdate);
//            result.setData(ret);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }
//
//    @RestGrpcMapping("/queryHdfsTable.do")
//    public NTSTATUS queryHdfsTable(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("params: {}", params);
//        try {
//            Params aa = params.driverObject(Params.class);
//            List<CostReportEntity> ret = dashboardService.queryHdfsTable(aa.ownerID, aa.ptdate);
//            result.setData(ret);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }
}
